package com.superb.common.feign.dynamic;

import org.springframework.cloud.openfeign.FeignClientBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * 动态Feign工厂类
 * @Author: ajie
 * @CreateTime: 2024-05-15 17:43
 */
@Component
public class DynamicFeignClientFactory {

    private FeignClientBuilder clientBuilder;

    public DynamicFeignClientFactory(ApplicationContext appContext) {
        this.clientBuilder = new FeignClientBuilder(appContext);
    }

    /**
     * 获取动态Feign类
     *
     * @param type       服务类
     * @param seviceName 服务名称
     * @param <T>
     * @return
     */
    public <T> T getFeignBySeviceName(Class<T> type, String seviceName) {
        return this.clientBuilder.forType(type, seviceName).build();
    }

    /**
     * 获取动态Feign类
     *
     * @param type 服务类
     * @param url  服务名称
     * @param <T>
     * @return
     */
    public <T> T getFeignByUrl(Class<T> type, String url) {
        return this.clientBuilder.forType(type, url).url(url).build();
    }
}
